package com.cloud.filter;

import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
//前置过滤器
public class GatewayFilter implements  GlobalFilter, Ordered {

    /**
     * Gateway 过滤器可以是全局的也可以是 断言过滤器，也就是针对于某一个跳转而设置的过滤器
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String url = exchange.getRequest().getPath().toString();
        int urlpath = url.indexOf("/payment/select/");
        if(urlpath != -1){
            int sub = url.lastIndexOf("/");
            String id = url.substring(sub+1, url.length());
            try{
                if(StringUtils.isBlank(id)){
                    //参数为空
                    exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
                    return exchange.getResponse().setComplete();
                }else{
                    Integer thisId = Integer.valueOf(id);
                }
            }catch (Exception e){
                //参数为空
                exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
                return exchange.getResponse().setComplete();
            }
            //如果有其他过滤器则继续下一个过滤器，无则进行相关业务跳转
        }

        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
